package org.terraz.lcs {
	import flash.display.Sprite;
	import flash.net.LocalConnection;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import bi.log.Logger;	

	/**
	 * @author TarGz
	 */
	public class Lcs extends Sprite {
		private static var _instance : Lcs;
		private const version : String = "0.2";
		private var initConnection : LocalConnection;
		private var id : String;
		private var tf : TextField;
		private var client : Client;

		public function Lcs(){
			_instance = this;
			Logger.clear();
			tf = new TextField() ;
			tf.x = 25 ;
			tf.y = 25 ;
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.multiline = true;
			tf.defaultTextFormat = new TextFormat("arial", 10) ;
			log("Starting LCB "+version+"...");
			addChild(tf) ;
			lookForMaster();
			
		}
		private function lookForMaster():void{
			Logger.debug("Lcs.lookForMaster()");
			initConnection = new LocalConnection();
            try {
            	Logger.debug("masterConnection : Established");
                initConnection.connect(ConnectionID.init);
				client = new Master();
            } catch (error:ArgumentError) {
                client = new Slave();
            }
            this.addChild(client);
            client.init();
		}

		 public static function getLcs() : Lcs{
		 	return _instance;
		 }

		public function log(m:String):void{
		 	Logger.log(m);
		 	tf.appendText("\n"+m );
		 }
	}

}
